import { login, logout, getInfo,loginBySms} from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
    state: {
        // token
        token: getToken(),
        // 姓名
        name: '',
        // 手机
        phone:'',
        // 头像
        avatar: '',
        // 角色
        roles: [],
        // 权限
        permissions: [],
        level: '',
        deptName:'',
        account:'',
        ifZjl:0,
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_PHONE: (state, phone) => {
            state.phone = phone
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        },
        SET_USERLEVEL: (state, level) => {
            state.level = level
        },
        SET_DEPTNANE: (state, deptName) => {
            state.deptName = deptName
        },
        SET_IF_ZJL: (state, v) => {
            state.ifZjl = v
        },
        SET_ACCOUNT: (state, v) => {
            state.account = v
        }
    },

    actions: {
        // 登录
        LoginByAccount({ commit }, userInfo) {
            const username = userInfo.account.trim()
            const password = userInfo.password
            const code = userInfo.code
            const uuid = userInfo.uuid
            return new Promise((resolve, reject) => {
                login(username, password, code, uuid).then(res => {
                    if(res && res.code===200){
                        setToken(res.data.token)
                        commit('SET_TOKEN', res.data.token)
                        // getInfo().then(res => {
                        //     const user = res.user
                        //     if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                        //         commit('SET_ROLES', res.roles)
                        //         commit('SET_PERMISSIONS', res.permissions)
                        //     } else {
                        //         commit('SET_ROLES', ['ROLE_DEFAULT'])
                        //     }
                        //     commit('SET_NAME', user.name)
                        //     commit('SET_PHONE', user.phone)
                        //     commit('SET_AVATAR', user.avatar)
                        //     commit('SET_USERLEVEL', user.dept.level)
                        //     commit('SET_DEPTNANE', user.dept.deptName)
                        //     resolve(res)
                        // }).catch(error => {
                        //     reject(error)
                        // })
                        resolve(res)
                    }else{
                        reject(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 短信登录
        LoginBySms({ commit }, userInfo) {
            const phone = userInfo.phone.trim()
            const code = userInfo.code
            return new Promise((resolve, reject) => {
                loginBySms(phone, code).then(res => {
                    if(res && res.code===200){
                        setToken(res.token)
                        commit('SET_TOKEN', res.token)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo(state.token).then(res => {
                    const data = res.data
                    if (data.roleKeys && data.roleKeys.length > 0) { // 验证返回的roles是否是一个非空数组
                        commit('SET_ROLES', data.roleKeys)
                        commit('SET_PERMISSIONS', data.permissions)
                        commit('SET_IF_ZJL', data.ifZjl)
                        commit('SET_ACCOUNT', data.account)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_NAME', data.useName)
                    // commit('SET_USERID', data.userId)
                    // commit('SET_USERDATA', data)//用户信息
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_AVATAR', '')
                    commit('SET_PHONE', '')
                    commit('SET_NAME', '')
                    commit('SET_TOKEN', '')
                    commit('SET_IF_ZJL', 0)
                    commit('SET_ROLES', [])
                    commit('SET_PERMISSIONS', [])
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user
